<template>
	<!-- 信息添加修改页面 -->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增信息' : '编辑信息'"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="type" label="类型">
        <ren-select v-model="dataForm.type" dict-type="massageType" placeholder="信息类型"></ren-select>
      </el-form-item>
      <el-form-item prop="status" label="状态">
        <ren-select v-model="dataForm.status" dict-type="massageStatus" placeholder="信息状态"></ren-select>
      </el-form-item>
      <el-form-item prop="massage" label="详情">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="dataForm.massage" placeholder="信息详情"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dialogVisible: false,
      dataForm: {
        id: '',
        massage: '',                    //信息状态：已读，未读
        type: '',                       //信息类型：订单、申请、提现
        status: '',                     //信息状态：已读，未读
        createDate: ''                  //创建时间
      }
    }
  },
  computed: {
    dataRule() {
      return {
        type: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        status: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    },
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/massage/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/massage', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
</style>
